<?php
/**
 * Created by PhpStorm.
 * User: litao
 * Date: 2017/12/22
 * Time: 21:00
 */

namespace App\Services;


class UCenterService
{

    private $ServerHttp = 'http://user.ht.cn/home/index/';//服务器地址
    private $MemHost = '119.29.32.50';    //服务器地址
    private $MemPort = 11211;        //服务器端口
    private $MemTime = 10;            //生命周期

    public function __construct()
    {

    }


    /**
     * 服务端注册2
     *
     * @param string $info 用户信息
     *
     * @return string 成功返回服务端id，失败返回-1，用户名重复返回0
     */
    function sys_regist($info)
    {

        $url = $this->ServerHttp . "regist";
        // return $url;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $info);
        curl_setopt($curl, CURLOPT_TIMEOUT, 2);   //设置超时时间为2秒
        $value = curl_exec($curl);
        curl_close($curl);
        return $value;
    }


    /**
     * 服务端登录
     *
     * @param string $info 用户信息
     *
     * @return string 成功返回iframe
     */
    function sys_login($info)
    {

        $url = $this->ServerHttp . "setUserInfo";

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $info);
        curl_setopt($curl, CURLOPT_TIMEOUT, 2);   //设置超时时间为2秒
        $value = curl_exec($curl);
        curl_close($curl);
        return $value;
    }

    /**
     *    退出触发事件
     */
    function sys_logout()
    {
        $url = $this->ServerHttp . "logout";
        // return $url;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 2);   //设置超时时间为2秒

        $value = curl_exec($curl);
        curl_close($curl);
        return $value;
    }

    /**
     * 修改用户信息
     *
     * @param array $info ,以字段为下标的用户信息，（要包括修改的的用户所属uid）
     *
     * @return boolean 成功返回true,失败返回false
     */
    function sys_update($info)
    {
        $bs = md5(time() . mt_rand(1000, 9999));
        while ($this->checkBs($bs)) {
            $bs = md5(time() . mt_rand(1000, 9999));
        }
        $this->SS($bs, $info);
        $url = $this->ServerHttp . "modifyInfo/bs/" . $bs;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 2);   //设置超时时间为2秒

        $value = curl_exec($curl);
        curl_close($curl);
        return $value;
    }


    //检测缓存标识是否存在
    function checkBs($bs)
    {
        if ($this->SS($bs)) {
            return 1;
        } else {
            return 0;
        }
    }

    /** 用户信息缓存
     *
     * @param string $key ,缓存名
     * @param string $val ,用户信息
     * @param int    $time ,生命周期
     *
     * @return 存值无返回，取值返回值，删除返回1，错误返回0，无法连接返回Could not connect
     */
    function SS($key, $val = '', $time)
    {
        if (empty($key)) {
            return 0;
        }

        $key = 'user' . $key;
        $mem = new \Memcache();
        $mem->connect($this->MemHost, $this->MemPort) or die ("Could not connect");
        if ($val === '') { // 获取缓存
            return json_decode($mem->get($key), true);

        } elseif (is_null($val)) { // 删除缓存
            return $mem->delete($key);
        }
        //有传$val,没传$time，读配置过期时间
        if (empty($time)) {
            $time = $this->MemTime;
        }
        //存值
        $val = json_encode($val);
        @$mem->set($key, $val, 0, $time);
        $mem->close();
    }

    //获取没有绑定手机邮箱登录的账号
    public function bangding($type, $username)
    {
        $users = file_get_contents($this->ServerHttp . "getusers/t/$type/u/$username");
        $user = json_decode($users, true);
        return $user;
    }

    //提交认证信息
    public function auth($data, $fun)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $this->ServerHttp . $fun);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        $value = curl_exec($curl);
        curl_close($curl);
        return $value;
    }


    /**
     * 获取认证信息
     * @param $uid
     *
     * @return array|mixed|\stdClass
     */
    public function getauth($uid)
    {

        $auth = file_get_contents($this->ServerHttp . "get_auth/u_id/$uid");
        return json_decode($auth, true);
    }


    /**
     * 获取是否存在已认证其他账号
     * @param $data
     * @param $type
     *
     * @return bool|string
     */
    public function alreadyauth($data, $type)
    {

        $re = file_get_contents($this->ServerHttp . "alreadyauth/sno/$data/type/$type");
        return $re;
    }



    /**
     * 开启手机号邮箱登录
     * @param $type email|phone
     * @param $uid
     *
     * @return bool|string
     */
    public function openlogin($type, $uid)
    {

        $re = file_get_contents($this->ServerHttp . "openlogin/type/$type/uid/$uid");
        return $re;
    }


    /**
     * 获取用户信息
     * @param $uid
     *
     * @return array|mixed|\stdClass
     */
    public function getuserinfos($uid)
    {

        $re = file_get_contents($this->ServerHttp . "getuserinfos/uid/$uid");
        return json_decode($re, true);
    }
}
